package cn.tqfeiyang.springplus.system.service.impl;

import cn.tqfeiyang.springplus.system.entity.RoleEntity;
import cn.tqfeiyang.springplus.system.mapper.RoleMapper;
import cn.tqfeiyang.springplus.system.service.RoleService;
import cn.tqfeiyang.springplus.system.request.RoleDTO;
import cn.tqfeiyang.springplus.system.response.RoleVO;
import cn.tqfeiyang.springplus.system.request.RoleSTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tqfeiyang.springplus.framework.conversion.ConversionService;
import cn.tqfeiyang.springplus.framework.exception.BusinessException;
import cn.tqfeiyang.springplus.framework.response.ResultCode;
import cn.tqfeiyang.springplus.framework.pagination.PageHelper;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;

/**
 * 角色服务实现类
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    /**
     * 对象转换服务
     */
    @Resource
    private ConversionService conversionService;

    /**
     * 角色映射接口
     */
    @Resource
    private RoleMapper roleMapper;

    /**
     * 分页查询角色
     *
     * @param roleSTO 查询数据
     * @return 角色列表
     */
    @Override
    public PageData<RoleVO> selectRolePage(RoleSTO roleSTO) {
        Assert.notNull(roleSTO, "roleSTO不能为空");
        PageHelper<RoleEntity> pageHelper = new PageHelper<>(roleSTO);
        return pageHelper.selectPage(roleMapper, RoleVO.class);
    }

    /**
     * 查询角色
     *
     * @param roleId 角色id
     * @return 角色信息
     */
    @Override
    public RoleVO selectRoleInfo(Long roleId) {
        Assert.notNull(roleId, "roleId不能为空");
        RoleEntity roleEntity = checkRoleExists(roleId);
        return conversionService.map(roleEntity, RoleVO.class);
    }

    /**
     * 新建角色
     *
     * @param roleDTO 请求数据
     * @return 角色id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRole(RoleDTO roleDTO) {
        Assert.notNull(roleDTO, "roleDTO不能为空");
        RoleEntity roleEntity = conversionService.map(roleDTO, RoleEntity.class);
        if (roleMapper.insert(roleEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_INSERT_ERROR);
        }
        return roleEntity.getRoleId();
    }

    /**
     * 修改角色
     *
     * @param roleId 角色id
     * @param roleDTO 请求数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(Long roleId, RoleDTO roleDTO) {
        Assert.notNull(roleId, "roleId不能为空");
        Assert.notNull(roleDTO, "roleDTO不能为空");
        RoleEntity originalEntity = checkRoleExists(roleId);
        RoleEntity roleEntity = conversionService.map(roleDTO, RoleEntity.class);
        roleEntity.setRoleId(roleId);
        roleEntity.setVersion(originalEntity.getVersion());
        if (roleMapper.updateById(roleEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_UPDATE_ERROR);
        }
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Long roleId) {
        Assert.notNull(roleId, "roleId不能为空");
        checkRoleExists(roleId);
        if (roleMapper.deleteById(roleId) == 0) {
            throw new BusinessException(ResultCode.ENTITY_DELETE_ERROR);
        }
    }

    /**
     * 检查角色实体是否存在
     *
     * @param roleId 角色id
     * @return 角色实体
     */
    protected RoleEntity checkRoleExists(Long roleId) {
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if (roleEntity == null) {
            throw new BusinessException(ResultCode.ENTITY_NOT_EXISTS);
        }
        return roleEntity;
    }

}
